<!--
Copyright 2011 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Author: Google TV Developer Relations
-->
<!DOCTYPE html>
<html lang="en">

<!--
Smart developers always View Source.

This application was built using Adobe Flex, an open source framework
for building rich Internet applications that get delivered via the
Flash Player or to desktops via Adobe AIR.

Learn more about Flex at http://flex.org
// -->

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AS3: Using Location Hash To Maintain State</title>
<style>
body {
  height: 720px;
  margin: 0px;
  overflow: hidden;
  width: 1280px;
}
</style>
</head>

<body scroll='no'>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
    id="flash_hash_demo" width="1280" height="720"
    codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
  <param name="movie" value="location-hash-content/flash_hash_demo.swf" />
  <param name="quality" value="high" />
  <param name="bgcolor" value="#869ca7" />
  <param name="allowScriptAccess" value="sameDomain" />
  <param name="allowFullScreen" value="true" />
  <embed src="location-hash-content/flash_hash_demo.swf" quality="high" bgcolor="#869ca7"
    width="1280" height="720" name="flash_hash_demo" align="middle"
    play="true"
    loop="false"
    quality="high"
    allowScriptAccess="sameDomain"
    allowFullScreen="true"
    type="application/x-shockwave-flash"
    pluginspage="http://www.adobe.com/go/getflashplayer">
  </embed>
</object>


<script type="text/javascript">
/**
 * SWF object used to call methods defined in AS3 by ExternalInterface.
 * @type Object
 */
var swfObj = document['flash_hash_demo'];

/**
 * Flag tells my onhashchange event handler whether my app should be updated
 * whenever hash value changes. Enabled by default. If hash change comes from
 * my app, use this flag to stop my onhashchange event handler from executing.
 * @type boolean
 */
var allowHashToUpdateFlash = true;

/**
 * Returns the value of the location hash.
 */
function getLocationHash () {
  return window.location.hash.substring(1);
}

/**
 * Called by AS3 Flash app to update location hash. Hash changes made through
 * this function should be ignored by the event handler.
 */
function setLocationHash(str) {
  // Tell the event handler to ignore this change since we manually updated it.
  allowHashToUpdateFlash = false;

  window.location.hash = str;
}

/**
 * Listen for hash changes. Use flag to determine whether hash changes should
 * propagate updates to my Flash app.
 */
window.onhashchange = function(e) {
  if (allowHashToUpdateFlash) {
    swfObj.updateApp(getLocationHash(), false);
  } else {
    allowHashToUpdateFlash = true;
  }
};
</script>

</body>
</html>
